//
//  ZJAlertView.m
//  ZJTY
//
//  Created by icholab on 2022/10/10.
//

#import "ZJAlertView.h"
#import "ZTDefine.h"

@implementation ZJAlertView

+ (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message cancel:(nullable NSString *)cancel affirm:(nullable NSString *)affirm WithController:(UIViewController *)viewController WithBlock:(nullable void (^)(NSInteger index))block{
    [self showAlertWithTitle:title message:message cancel:cancel affirm:affirm textAlignment:NSTextAlignmentCenter WithController:viewController WithBlock:block];
}

+ (void)showAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message cancel:(nullable NSString *)cancel affirm:(nullable NSString *)affirm textAlignment:(NSTextAlignment)textAlignment WithController:(UIViewController *)viewController WithBlock:(nullable void (^)(NSInteger index))block{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    if (message) {
        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
        paraStyle.alignment = textAlignment;
        NSMutableAttributedString *atrStr = [[NSMutableAttributedString alloc] initWithString:message attributes:@{NSParagraphStyleAttributeName:paraStyle,NSFontAttributeName:[UIFont systemFontOfSize:16.0]}];
        [alert setValue:atrStr forKey:@"attributedMessage"];
    }
    
    if (cancel) {
        UIAlertAction *centain = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (block) {
                block(0);
            }
        }];
        [alert addAction:centain];
        [centain setValue:UIColorFromRGB(0x969799) forKey:@"titleTextColor"];
    }
    if (affirm) {
        UIAlertAction *affirmAction = [UIAlertAction actionWithTitle:affirm style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (block) {
                block(1);
            }
        }];
        [alert addAction:affirmAction];
        [affirmAction setValue:UIColorFromRGB(0x323233) forKey:@"titleTextColor"];
    }
    [viewController presentViewController:alert animated:YES completion:nil];
}

+ (void)showSheetWithTitle:(nullable NSString *)title message:(nullable NSString *)message cancel:(nullable NSString *)cancel actionStrs:(NSArray *)actionStrs WithController:(UIViewController *)viewController WithBlock:(nullable void (^)(NSInteger index,NSString *title))block{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
//    alert.view.backgroundColor = UIColorFromRGB(0x282828);
    if (cancel) {
        UIAlertAction *centain = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:centain];
        [centain setValue:UIColorFromRGB(0x969799) forKey:@"titleTextColor"];
//        [centain setValue:UIColorFromRGB(0x282828) forKey:@"backgroundColor"];
    }
    for (int i=0; i<actionStrs.count; i++) {
        NSString *action = actionStrs[i];
        UIAlertAction *affirmAction = [UIAlertAction actionWithTitle:action style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (block) {
                block(i+1,action.title);
            }
        }];
        [alert addAction:affirmAction];
        [affirmAction setValue:UIColorFromRGB(0x323233) forKey:@"titleTextColor"];
    }
    [viewController presentViewController:alert animated:YES completion:nil];
}
@end
